/**
 * TrayIconPopup.java 2008-05-16
 * 
 * Linker is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */

package linker.gui;

import java.awt.CheckboxMenuItem;
import java.awt.Menu;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JOptionPane;

import org.jivesoftware.smack.packet.Presence;

import linker.LocalVariablesManager;
import linker.status.StatusManager;

/**
 * The popup menu refer to the traicon on the desktop's tray.
 * 
 * @version 2008-05-20
 * @author Jianfeng tujf.cn@gmail.com
 * @author Tan tan1986@gmail.com
 * 
 */

public class TrayIconPopup extends PopupMenu implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * The mainWindow refer.
	 */
	private MainWindow mainWindow;

	/**
	 * The checkbox to display the mainWindow.
	 */
	private CheckboxMenuItem showCheckBox;

	/**
	 * The Status info.
	 * 
	 * TODO translae. :)
	 */
	private String statusLabel = LocalVariablesManager
	.getLocalVariables("TrayIconPopup.PopupMenu.Status");

	/**
	 * Online info.
	 */
	private String onlineLabel = LocalVariablesManager
	.getLocalVariables("TrayIconPopup.PopupMenu.Status.Online");

	/**
	 * Busy info.
	 */
	private String busyLabel = LocalVariablesManager
	.getLocalVariables("TrayIconPopup.PopupMenu.Status.Busy");

	/**
	 * Away info.
	 */
	private String awayLabel = LocalVariablesManager
	.getLocalVariables("TrayIconPopup.PopupMenu.Status.Away");

	/**
	 * Setting info.
	 */
	private String settingLabel = LocalVariablesManager
	.getLocalVariables("TrayIconPopup.PopupMenu.Setting");

	/**
	 * 
	 * @param window
	 *            The mainWindo refer.
	 */
	public TrayIconPopup(final MainWindow window) {
		super();

		this.mainWindow = window;

		// Setting
		MenuItem item = new MenuItem(this.settingLabel);
		item.addActionListener(this);
		add(item);

		// Status
		Menu menu = new Menu(this.statusLabel);
		item = new MenuItem(this.onlineLabel);
		item.addActionListener(this);
		menu.add(item);
		item = new MenuItem(this.busyLabel);
		item.addActionListener(this);
		menu.add(item);
		item = new MenuItem(this.awayLabel);
		item.addActionListener(this);
		menu.add(item);
		add(menu);

		// Show.
		showCheckBox = new CheckboxMenuItem(LocalVariablesManager
				.getLocalVariables("TrayIconPopup.PopupMenu.Show"));
		showCheckBox.setState(true);
		showCheckBox.addItemListener(new ItemListener() {
			public void itemStateChanged(final ItemEvent e) {
				int showState = e.getStateChange();
				if (showState == ItemEvent.SELECTED) {
					window.setVisible(true);
				} else {
					window.setVisible(false);
				}
			}
		});
		add(showCheckBox);

		// Exit.
		item = new MenuItem(LocalVariablesManager
				.getLocalVariables("TrayIconPopup.PopupMenu.Exit"));
		item.addActionListener(this);
		add(item);

	}

	/**
	 * 
	 * @param e
	 *            ActionEvent.
	 */
	public final void actionPerformed(final ActionEvent e) {
		if (e.getActionCommand() == LocalVariablesManager
				.getLocalVariables("TrayIconPopup.PopupMenu.Exit")) {
			int op = JOptionPane.showConfirmDialog(mainWindow,
					LocalVariablesManager
							.getLocalVariables("MainWindow.ExitConfirm.Info"),
					LocalVariablesManager
							.getLocalVariables("MainWindow.ExitConfirm.Title"),
					JOptionPane.OK_CANCEL_OPTION);
			if (op == JOptionPane.OK_OPTION) {
				System.exit(0);
			}
		} else if (e.getActionCommand() == LocalVariablesManager
				.getLocalVariables("TrayIconPopup.PopupMenu.Show")) {
			mainWindow.setVisible(true);
		} else if (e.getActionCommand() == this.awayLabel) {
			StatusManager.getManager(null).setStatus(Presence.Mode.away, null);
		} else if (e.getActionCommand() == this.busyLabel) {
			StatusManager.getManager(null).setStatus(Presence.Mode.dnd, null);
		} else if (e.getActionCommand() == this.onlineLabel) {
			StatusManager.getManager(null).setStatus(Presence.Mode.available,
					null);
		} else if (e.getActionCommand() == this.settingLabel) {
			SettingsWindow.getSettingWindow().setVisible(true);
		}
	}

	/**
	 * State ture check "show".
	 * 
	 * @param state
	 *            The visible of mainWindow.
	 */
	public final void setShowCheckBox(final boolean state) {
		showCheckBox.setState(state);
	}

}
